"""
处理封装相关数据
"""
import re

from excutor.excutor import Excutor

class Package(Excutor):
    def __init__(self, data):
        super().__init__(data)
        self.package = '封装'
    
    @staticmethod
    def Existence(data,package = '封装'):
        return data[data['new_key'].str.contains(package)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexSize = self.data[self.data['new_key'].str.contains(self.package)].index
        for index in indexSize:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data
    
    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        old_value = oneRowData['old_value']
        old_value = re.split('[Ll]ead *|[Bb]all *|\(.*?\)|[0-9] ?[Xx] ?[0-9]',old_value)
        old_value = ''.join(old_value)
        old_value = old_value.strip('[').strip(']')
        packageList = old_value.replace('，',',').split(',')
        newList = []
        for package in packageList:
            numbers = re.findall('[0-9]+[A-Za-z]?',package)
            alphas = re.findall('[A-Z]+',package)
            newList.append('-'.join(alphas+numbers))
        return ','.join(newList)
        